//引入组件库
var do_Page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var hashdata = mm("do_HashData");
var communityUtil = require("helper/community");
var openHelper = require("helper/open");
var root = ui("$");

var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);
var noData = [{"template":1}];
var do_ALayout_filter=ui("do_ALayout_filter");
var interests="";

do_ALayout_filter.on("touch","",300,function(){
	do_Page.hideKeyboard();
	do_Page.fire("interests");
	do_Page.fire("getInterests",{"initSelect":interests});
});
//选择爱好后返回刷新
do_Page.on("refreshInterest",function(data){
	if(null!=data&&''!=data.interests){
		ui("do_Label_1").text= data.interests;
	}else{
		ui("do_Label_1").text= "筛选";
	}
	interests = data.interests;
	binddata();
});
var noJoinData = [{"template":1,'labelContent':"请先加入社区"}];
// andriod返回键
do_Page.on("back", function() {
	var deviceUtil = require("helper/device");
	deviceUtil.home();
});

// 当前页码
var page = 1;
// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.base.neighborPage;

//var communityHeight=communityUtil.communityHeight();
//ui("aLayoutRoot").height=communityHeight;
//ui("aLayoutRoot").redraw();
//ui("listView").height=communityHeight;
ui("listView").redraw();
// 全部刷新数据
function binddata() {
	page = 1;
	var params = {
		page : page,
		communityId:communityUtil.communityVisitId(),
		interests:interests
	}
	qqHttp.post(httpUrl, params, function(data) {
		listview.rebound();
		listdata.removeAll();// 清除数据
		if(!data.isJoin){
			listdata.addData(noJoinData);
			listview.refreshItems();// 刷新item数据
			return;
		}
		if (data.page) {
			if(data.page.content.length){
				for (var i = 0; i < data.page.content.length; i++) {
					if(i==0){
						data.page.content[i].showTopLine=true;
					}else{
						data.page.content[i].showTopLine=false;
					}
				}
				listdata.addData(data.page.content);
			}else{
				listdata.addData(noData);
			}
			listview.refreshItems();// 刷新item数据
		}
	});
}
function getNextPageData() {
		page++;
		var params = {
			page : page,
			communityId:communityUtil.communityVisitId(),
			interests:interests
		}
		qqHttp.post(httpUrl, params, function(data) {
			if(!data.isJoin){
				listdata.addData(noJoinData);
				listview.refreshItems();// 刷新item数据
				return;
			}
			if (data.page) {
				listview.rebound();
				listdata.addData(data.page.content);
				listview.refreshItems();
			}
		});
}

// 下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		binddata();
	}
});

// 上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 1) {
		getNextPageData();
	}
});
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	binddata();
});
//访问其他社区
/*do_Page.on("community_change_join", function(data) {
	binddata();
});*/
//tab点击
do_Page.on("community_neighbor_tab_touch", function(data) {
	binddata();
});
//点击一个用户
listview.on("touch","",300,function(data){
	var user = listdata.getOne(data);
	var userId = user.id;
	if(userId){
		openHelper.openPersonal(userId);
	}
});
